package org.rency.ues.engine.converter;

import org.rency.ues.common.enums.EncryptType;
import org.rency.ues.dal.entity.EnDataDO;
import org.rency.ues.domain.model.EnDataDomain;
import org.rency.ues.domain.request.EncryptRequest;

import java.util.Date;

/**
 * ${todo}
 *
 * @author: Created by user_rcy@163.com on 2016/8/3.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public class EncryptConverter {

    public static EnDataDomain build(String ticket, EncryptRequest request){
        EnDataDomain domain = new EnDataDomain();
        domain.setEncryptKey(request.getEncryptedKey());
        domain.setEncryptData(request.getEncryptedData());
        domain.setCreateTime(new Date());
        domain.setUpdateTime(new Date());
        domain.setTicket(ticket);
        domain.setEncryptType(request.getEncryptType());
        domain.setSerialNo(request.getSerialNo());
        return domain;
    }

    public static EnDataDO convert(EnDataDomain domain){
        if(domain == null){
            return null;
        }
        EnDataDO entity = new EnDataDO();
        entity.setId(domain.getId());
        entity.setEncryptKey(domain.getEncryptKey());
        entity.setEncryptData(domain.getEncryptData());
        entity.setCreateTime(domain.getCreateTime());
        entity.setUpdateTime(domain.getUpdateTime());
        entity.setTicket(domain.getTicket());
        entity.setEncryptType(domain.getEncryptType().getCode());
        entity.setSerialNo(domain.getSerialNo());
        return entity;
    }

    public static EnDataDomain convert(EnDataDO entity){
        if(entity == null){
            return null;
        }
        EnDataDomain domain = new EnDataDomain();
        domain.setId(entity.getId());
        domain.setEncryptKey(entity.getEncryptKey());
        domain.setEncryptData(entity.getEncryptData());
        domain.setCreateTime(entity.getCreateTime());
        domain.setUpdateTime(entity.getUpdateTime());
        domain.setTicket(entity.getTicket());
        domain.setEncryptType(EncryptType.get(entity.getEncryptType()));
        domain.setSerialNo(entity.getSerialNo());
        return domain;
    }
}
